How To Install PostgreSQL on Ubuntu 22.04
In this guide, we will explain how to install PostgreSQL on Ubuntu 22.04, enabling you to set up a powerful relational database system for your projects.
PostgreSQL is one of the leading and most widely used open-source relational database management systems. It is a robust and high-performance database system known for its flexibility in handling multiple data types, stability, integrity, and concurrency.
Prerequisites
Make sure you have the necessary prerequisites to successfully install PostgreSQL on Ubuntu 22.04:
- Launch the Ubuntu 22.04 instance on RackBank.
- Access the server using SSH.
Step 1: Update & Upgrade the System
First, ensure your package list is up to date to avoid any issues with outdated packages:
sudo apt-get update -y
sudo apt-get upgrade -y
Step 2: Install PostgreSQL
To install PostgreSQL, use the apt package manager and run the following command from a terminal prompt:
sudo apt install postgresql postgresql-contrib -y
Step 3: Start and Enable PostgreSQL
After installation, the PostgreSQL service starts and is enabled automatically.
Start the PostgreSQL server:
sudo systemctl start postgresql
Enable it to start when the system reboots:
sudo systemctl enable postgresql
Step 4: Check the Status
Verify that the database service is active and running:
sudo systemctl status postgresql
Step 5: Check the Version
Verify the version of PostgreSQL installed by running the command:
sudo psql --version
Step 6: Manage PostgreSQL Service
PostgreSQL runs quietly in the background upon installation. By default, it listens on TCP port 5432. You can verify this using the ss
command:
sudo ss -antpl | grep 5432
Step 7: Access PostgreSQL Prompt
When PostgreSQL is installed, a standard user account named postgres
is automatically created. The postgres
user takes on the default PostgreSQL role as well.
Log in as the user postgres
:
sudo su - postgres
Access the PostgreSQL prompt by typing:
psql
From here, you can start executing your database management tasks.
To leave the prompt, type:
\q
Exiting the prompt will return you to your postgres
account in the terminal. To return to your normal account, execute the exit
command:
exit
Step 8: Create a New Database
This section covers the process of establishing a database in PostgreSQL and generating tables within it. The standard PostgreSQL installation includes three pre-existing databases: postgres
, template0
, and template1
.
To list the existing databases, run:
\l
To create a new database, use the following command:
create database example_db;
Step 9: Create a New Table
To make a new table and add data, first switch to the desired database where the table will be stored using the \c
command:
\c example_db
After transitioning to the database, you can create tables. The syntax for creating a table in SQL is as follows:
For example, to generate a table named employees
with six specific columns:
CREATE TABLE employees (
Person_ID serial PRIMARY KEY,
First_Name VARCHAR (50) NOT NULL,
Last_Name VARCHAR (50) NOT NULL,
Email VARCHAR (255) UNIQUE NOT NULL,
City VARCHAR (50) NOT NULL,
Age INT NOT NULL
);
Step 10: Verify Table Creation
To check the status of the created table in a database, use the \dt
command:
\dt
You can also view the table schema using the following syntax:
\d table_name
Step 11: Insert Data into the Table
To insert values into the table, use the INSERT INTO table_name
command:
INSERT INTO employees VALUES (001, 'Phil', 'Shaw', 'philshaw@gmail.com', 'New York', 40);
Step 12: Check Table Records
To view the records in the table, run the following command:
SELECT * FROM employees;